<?php
function Query($sql)
{
    $conn=mysql_connect('localhost:3306','root','') or die('Loi ket noi!');
    mysql_select_db('qldh') or die ('Loi CSDL');
    $data = mysql_query($sql,$conn) or die('Loi truy van');
    mysql_close($conn);
    return $data;
}
     
    if(isset($_GET['action']))
    {
        $action = $_GET['action'];
    }else $action='';
 
    if(isset($_GET['id']))
    {
        $id = $_GET['id'];
    }else $id=0;
 
 
?>
<html>
<head>
<title> Quan ly Dai hoc.</title>
</head>
<body>
<h3>QUAN LY SINH VIEN</h3>
<?php
switch($action)
{
    case 'new':
        ?>
        <h4>THEM SINH VIEN</h4>
<form action="qldh.php?action=insert" method="POST" enctype="">
<table>
<tr><td>Ho ten:</td> <td><input type="text" name="ho_ten" value=""></td></tr>
<tr><td>Ngay sinh:</td> <td><input type="text" name="ngay_sinh" value="1990-02-25"></td></tr>
<tr><td>Gioi tinh:</td> <td>
<input type="radio" name="gioi_tinh" value="True"> Nam
<input type="radio" name="gioi_tinh" value="False" checked> Nu
</td></tr>
<tr><td>Anh:</td> <td>
<img src="hieutt.jpg" alt="Anh sinh vien" width="150" heigh="200">
<br><input type="file" name="anh">
</td></tr>
<tr><td>Dia chi:</td> <td>
<input type="text" name="dia_chi" value="">
</td></tr>
<tr><td></td> <td></td></tr>
<tr><td>Dien thoai:</td> <td>
<input type="text" name="dien_thoai" value="">
</td></tr>
<tr><td>So thich:</td> <td>
<input type="checkbox" name="so_thich[]" value="1" checked> The thao
<input type="checkbox" name="so_thich[]" value="2"> Du lich
<input type="checkbox" name="so_thich[]" value="4" checked> Phim anh
</td></tr>
<tr><td>Email:</td> <td>
<input type="text" name="email" value="">
</td></tr>
<tr><td>Lop:</td> <td>
<select name="lop">
<?php 
    $l_rows  =Query('Select * from lop');
    while($l_row = mysql_fetch_array($l_rows))
    {
    ?>
<option value="<?php echo $l_row['id']?>">
<?php echo $l_row['name']?> </option>
 
<?php
    }
?>
</select>
</td></tr>
 
<tr><td>Mo ta:</td> <td>
<textarea name="mo_ta"> </textarea></td></tr>
<tr>
<td><input type="submit" value="Them sinh vien"></td> 
<td><input type="reset" value="Nhap lai"></td></tr>
 
</table>
 
</form>
         
         
        <?php
        break;
 
    case 'insert':
    //print_r($_POST);
 
    $hoten = $_POST['ho_ten'];
    $ngaysinh = $_POST['ngay_sinh'];
    $gioitinh = $_POST['gioi_tinh'];
    $diachi = $_POST['dia_chi'];
    $dienthoai = $_POST['dien_thoai'];
    $sothich = 0;
    foreach($_POST['so_thich'] as $v)
        $sothich += $v;
    $email = $_POST['email'];
    $mota = $_POST['mo_ta'];
    $lop = $_POST['lop'];
 
    $sql = "insert into sinh_vien (
    fullname,
DOB,
sex,
address,
phone_number,
hobby,
email,
class_id,
description) values ('$hoten','$ngaysinh',$gioitinh,'$diachi','$dienthoai',$sothich,'$email',$lop,'$mota')";
Query($sql);
        break;
    case 'edit':
        $sql =  "select * from sinh_vien where id = $id";
        $sv = Query($sql);
        $sv = mysql_fetch_array($sv);
         
?>
 
        <h4>SUA SINH VIEN</h4>
<form action="qldh.php?action=update&id=<?php echo $id;?>" method="POST" enctype="">
<table>
<tr><td>Ho ten:</td> <td><input type="text" name="ho_ten" value="<?php echo $sv['fullname'];?>"></td></tr>
<tr><td>Ngay sinh:</td> <td><input type="text" name="ngay_sinh" value="<?php echo $sv['DOB'];?>"></td></tr>
<tr><td>Gioi tinh:</td> <td>
<input type="radio" name="gioi_tinh" value="True" <?php echo($sv['sex']?'checked':'');?>> Nam
<input type="radio" name="gioi_tinh" value="False" <?php echo($sv['sex']?'':'checked');?>> Nu
</td></tr>
<tr><td>Anh:</td> <td>
<img src="hieutt.jpg" alt="Anh sinh vien" width="150" heigh="200">
<br><input type="file" name="anh">
</td></tr>
<tr><td>Dia chi:</td> <td>
<input type="text" name="dia_chi" value="<?php echo $sv['address'];?>">
</td></tr>
<tr><td></td> <td></td></tr>
<tr><td>Dien thoai:</td> <td>
<input type="text" name="dien_thoai" value="<?php echo $sv['phone_number'];?>">
</td></tr>
<tr><td>So thich:</td> <td>
<input type="checkbox" name="so_thich[]" value="1" <?php echo(($sv['hobby']&1)?'checked':'');?>> The thao
<input type="checkbox" name="so_thich[]" value="2" <?php echo(($sv['hobby']&2)?'checked':'');?>> Du lich
<input type="checkbox" name="so_thich[]" value="4" <?php echo(($sv['hobby']&4)?'checked':'');?>> Phim anh
</td></tr>
<tr><td>Email:</td> <td>
<input type="text" name="email" value="<?php echo $sv['email'];?>">
</td></tr>
<tr><td>Lop:</td> <td>
<select name="lop">
<?php 
    $l_rows  =Query('Select * from lop');
    while($l_row = mysql_fetch_array($l_rows))
    {
    ?>
<option value="<?php echo $l_row['id']?>"
<?php echo ($sv['class_id']==$l_row['id']?'selected':''); ?>
>
<?php echo $l_row['name']?> </option>
 
<?php
    }
?>
</select>
</td></tr>
 
<tr><td>Mo ta:</td> <td>
<textarea name="mo_ta">
<?php echo $sv['description'];?> </textarea></td></tr>
<tr>
<td><input type="submit" value="Sua sinh vien"></td> 
<td><input type="reset" value="Nhap lai"></td></tr>
 
</table>
 
</form>
<?php
        break;
 
    case 'update':
    $hoten = $_POST['ho_ten'];
    $ngaysinh = $_POST['ngay_sinh'];
    $gioitinh = $_POST['gioi_tinh'];
    $diachi = $_POST['dia_chi'];
    $dienthoai = $_POST['dien_thoai'];
    $sothich = 0;
    if(is_array($_POST['so_thich']))
    {
        foreach($_POST['so_thich'] as $v)
            $sothich += $v;
    }
    $email = $_POST['email'];
    $mota = $_POST['mo_ta'];
    $lop = $_POST['lop'];
    $sql = "update sinh_vien set
    fullname='$hoten',
    DOB='$ngaysinh',
    sex=$gioitinh,
    address='$diachi',
    phone_number='$dienthoai',
    hobby=$sothich,
    email='$email',
    class_id=$lop,
    description='mota'
    where id = $id";
    Query($sql);
        break;
    case 'delete':
        $sql = "delete from sinh_vien where id=$id";
        Query($sql);
 
        break;
     
}
 
?>
 
<a href="qldh.php?action=new">Them sinh vien</a>
<table border="1">
<tr>
<td><b>Ma SV</b></td>
<td><b>Ho ten</b></td>
<td><b>Ngay sinh</b></td>
<td><b>Gioi tinh</b></td>
<td><b>Dia chi</b></td>
<td><b>Dien thoai</b></td>
<td><b>So thich</b></td>
<td><b>Email</b></td>
<td><b>Lop</b></td>
<td><b>Mo ta</b></td>
<td><b>Thao tac</b></td>
</tr>
<?php
    $sql = 'select sv.*,l.name as lop from
    sinh_vien sv, lop l
    where sv.class_id = l.id';
    $rows = Query($sql);
     
    while($row = mysql_fetch_array($rows))
    {
?>
<tr>
<td> <?php echo $row['id'];?></td>
<td> <?php echo $row['fullname'];?></td>
<td> <?php echo $row['DOB'];?></td>
<td> <?php echo ($row['sex']?'Nam':'Nu');?></td>
<td> <?php echo $row['address'];?></td>
<td> <?php echo $row['phone_number'];?></td>
<td> 
<?php echo (($row['hobby']&1)?'The thao':'');?>
<?php echo (($row['hobby']&2)?'Du lich':'');?>
<?php echo (($row['hobby']&4)?'Phim anh':'');?>
</td>
<td> <?php echo $row['email'];?></td>
<td> <?php echo $row['lop'];?></td>
<td> <?php echo $row['description'];?></td>
<td>
<a href="qldh.php?action=edit&id=<?php echo $row['id'];?>">Sua</a> | 
<a href="qldh.php?action=delete&id=<?php echo $row['id'];?>">Xoa</a>
 
</td>
</tr>
<?php } ?>
</table>
 
 
</body>
</html>